<template>
    <div class="open">
<div class="open-box">


        <div class="app-container">
            <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/"
                :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </div>
        <div >
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                class="demo-ruleForm">
                <el-form-item label="用户名"  prop="account">
                    <el-input disabled v-model="ruleForm.account" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="旧密码" prop="oldPwd">
                    <el-input  v-model="ruleForm.oldPwd" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="newPwd">
                    <el-input  v-model="ruleForm.newPwd" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input  v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                </el-form-item>
            </el-form>
        </div>
</div>
    </div>

</template>
    
<script>
import {editPwd} from '@/api/user'
export default {
    data() {
      
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.newPwd) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
        return {
            imageUrl: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
            ruleForm: {
                account: '', //用户名
                oldPwd: '', //旧密码
                newPwd:'', //新密码
                checkPass: '', //确认密码
                userInfo:localStorage.getItem('adminAccountVO') 
        },
        rules: {
            newPwd: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ]
        }
      };
    },
    created(){
        this.userInfo = JSON.parse(localStorage.getItem('adminAccountVO')) 
        this.ruleForm.account = this.userInfo.account
      

    },
    methods: {
        btn(){
            // this.$router.push('/login')
            this.$store.dispatch('user/resetToken').then(() => {
            location.reload()
          })
        },
       submitForm(formName) {
        this.$refs[formName].validate( async (valid)  => {
          if (valid) {
            let data = {
                account:this.ruleForm.account,
                oldPwd:this.ruleForm.oldPwd,
                newPwd:this.ruleForm.newPwd,
                accountId:this.userInfo.accountId
            }
         let result = await   editPwd(data)
         if(result.code==0){
            localStorage.removeItem('accessToken')
            localStorage.removeItem('adminAccountVO')
            this.$router.push('/login')
         }

      
          } else {
      
            return false;
          }
        });
      },
     
        handleAvatarSuccess(res, file) {
            this.imageUrl = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        }
    }
}
</script>
    
<style lang="scss" scoped>
.open{
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    .open-box{
      text-align: center;
    }
}
.app-container{
    margin-left: 50px;
}


  ::v-deep .el-input__inner { 
    -webkit-appearance: none;
    background-color: #FFF;
    background-image: none;
    border-radius: 4px;
    border: 1px solid #DCDFE6;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    color: #606266;
    display: inline-block;
    font-size: inherit;
    height: 40px;
    line-height: 40px;
    outline: 0;
    padding: 0 15px;
    -webkit-transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    width: 300px;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    margin-left: 50px;
    // display: block;
    border-radius: 50%;
}
</style>